/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package datos;

/**
 *
 * @author MAS-NATURAL
 */
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Cifrador
{
  public String encriptar(String clearText)
  {
    byte[] secret = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
    Key key = null;
    String cipherTextB64 = "";
    try
    {
      SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
      key = skf.generateSecret(new DESedeKeySpec(secret));

      Cipher cipher = Cipher.getInstance("DESede");

      cipher.init(1, key);
      byte[] cipherText = cipher.doFinal(clearText.getBytes());

      BASE64Encoder base64encoder = new BASE64Encoder();
      cipherTextB64 = base64encoder.encode(cipherText);
    }
    catch (NoSuchAlgorithmException e)
    {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }
    catch (InvalidKeyException e) {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }
    catch (NoSuchPaddingException e) {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }
    catch (IllegalBlockSizeException e) {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }
    catch (BadPaddingException e) {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }
    catch (InvalidKeySpecException e) {
      new Log("Se genero un error en cifrador/encriptar" + e);
    }

    return cipherTextB64;
  }

  public String desencriptar(String cipherTextB64)
  {
    byte[] secret = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
    Key key = null;

    String clearText = "";
    try
    {
      SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
      key = skf.generateSecret(new DESedeKeySpec(secret));

      Cipher cipher = Cipher.getInstance("DESede");

      BASE64Decoder base64decoder = new BASE64Decoder();
      byte[] cipherText = base64decoder.decodeBuffer(cipherTextB64);

      cipher.init(2, key);
      byte[] bclearText = cipher.doFinal(cipherText);
      clearText = new String(bclearText);
    }
    catch (NoSuchAlgorithmException e)
    {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (NoSuchPaddingException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (InvalidKeyException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (IllegalBlockSizeException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (BadPaddingException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (IOException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }
    catch (InvalidKeySpecException e) {
      new Log("Se genero un error en cifrador/desencriptar" + e);
    }

    return clearText;
  }
}